﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Framework.Common
{
	public class CommonUpload
	{
		///   <summary> 
		///   生成缩略图 
		///   </summary> 
		///   <param   name= "originalImagePath "> 源图路径（物理路径） </param> 
		///   <param   name= "thumbnailPath "> 缩略图路径（物理路径） </param> 
		///   <param   name= "width "> 缩略图宽度 </param> 
		///   <param   name= "height "> 缩略图高度 </param> 
		///   <param   name= "mode "> 生成缩略图的方式 </param>         
		public static void MakeThumbnail2(string originalImagePath, string thumbnailPath, int width, int height, string mode)
		{
			System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

			int towidth = width;
			int toheight = height;

			int x = 0;
			int y = 0;
			int ow = originalImage.Width;
			int oh = originalImage.Height;

			switch (mode.Trim())
			{
				case "HW"://指定高宽缩放（可能变形）                                 
					break;
				case "W"://指定宽，高按比例                                         
					toheight = originalImage.Width / 2;
					oh = ow / 2;
					break;
				case "H"://指定高，宽按比例 
					ow = oh * 2;
					towidth = originalImage.Height * 2;
					break;
				default:
					break;
			}
			x = (towidth - originalImage.Width) / 2; y = (toheight - originalImage.Height) / 2;


			//新建一个bmp图片 
			System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

			//新建一个画板 
			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

			//设置高质量插值法 
			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

			//设置高质量,低速度呈现平滑程度 
			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

			//清空画布并以透明背景色填充 
			g.Clear(System.Drawing.Color.White);

			//在指定位置并且按指定大小绘制原图片的指定部分 
			g.DrawImage(originalImage, new System.Drawing.Rectangle(x, y, ow, oh), new System.Drawing.Rectangle(0, 0, towidth, toheight), System.Drawing.GraphicsUnit.Pixel);

			try
			{
				//以jpg格式保存缩略图 
				bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

			}
			catch (System.Exception e)
			{
				originalImage.Dispose();
				bitmap.Dispose();
				g.Dispose();
				throw e;
			}
			finally
			{
				originalImage.Dispose();
				bitmap.Dispose();
				g.Dispose();
			}
		}

		///   <summary> 
		///   生成缩略图 
		///   </summary> 
		///   <param   name= "originalImagePath "> 源图路径（物理路径） </param> 
		///   <param   name= "thumbnailPath "> 缩略图路径（物理路径） </param> 
		///   <param   name= "width "> 缩略图宽度 </param> 
		///   <param   name= "height "> 缩略图高度 </param> 
		///   <param   name= "mode "> 生成缩略图的方式 </param>         
		public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
		{
			System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

			int towidth = width;
			int toheight = height;

			int x = 0;
			int y = 0;
			int ow = originalImage.Width;
			int oh = originalImage.Height;

			switch (mode.Trim())
			{
				case "HW"://指定高宽缩放（可能变形）                                 
					break;
				case "W"://指定宽，高按比例                                         
					toheight = originalImage.Height * width / originalImage.Width;
					break;
				case "H"://指定高，宽按比例 
					towidth = originalImage.Width * height / originalImage.Height;
					break;
				case "Cut"://指定高宽裁减（不变形）                                 
					if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
					{
						oh = originalImage.Height;
						ow = originalImage.Height * towidth / toheight;
						y = 0;
						x = (originalImage.Width - ow) / 2;
					}
					else
					{
						ow = originalImage.Width;
						oh = originalImage.Width * height / towidth;
						x = 0;
						y = (originalImage.Height - oh) / 2;
					}
					break;
				default:
					break;
			}

			//新建一个bmp图片 
			System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

			//新建一个画板 
			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

			//设置高质量插值法 
			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

			//设置高质量,低速度呈现平滑程度 
			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

			//清空画布并以透明背景色填充 
			g.Clear(System.Drawing.Color.Transparent);

			//在指定位置并且按指定大小绘制原图片的指定部分 
			g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
					new System.Drawing.Rectangle(x, y, ow, oh),
					System.Drawing.GraphicsUnit.Pixel);

			try
			{
				//以jpg格式保存缩略图 
				bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

			}
			catch (System.Exception e)
			{
				originalImage.Dispose();
				bitmap.Dispose();
				g.Dispose();
				throw e;
			}
			finally
			{
				originalImage.Dispose();
				bitmap.Dispose();
				g.Dispose();
			}
		}

		/// <summary>
		/// 多图片生成PDF
		/// </summary>
		/// <param name="strImagePath">图片路径数据</param>
		/// <param name="FilePath">文件保存路径</param>
		/// <returns></returns>
		public static bool CreatePDF(List<string> strImagePath, string FilePath)
		{
			//创建Document对象，默认大小为A4
			Document pdfDocument = new Document();
			//打开并创建新的PDF文件
			PdfWriter.GetInstance(pdfDocument, new FileStream(FilePath, FileMode.Create));
			//打开上下文
			try
			{
				pdfDocument.Open();
				int iHeight = 460;//高度
				int iWidth = 480;//宽度
				foreach (string s in strImagePath)
				{
					// 获取image对象
					iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(new Uri(s));
					//设置图片对其方式
					img.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
					//设置图片的相对大小，未超过指定高宽按原图尺寸
					if (img.ScaledHeight > iHeight && img.ScaledWidth > iWidth)
					{
						img.ScaleAbsolute(iWidth, iHeight);
					}
					//添加新的文件               
					pdfDocument.Add(img);
				}
				pdfDocument.Close();
				return true;
			}
			catch
			{
				return false;
			}
		}

		/// <summary>
		/// 裁剪图片并存储
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="w"></param>
		/// <param name="h"></param>
		/// <param name="path"></param>
		/// <param name="cutPath"></param>
		public void CutImage(int x, int y, int w, int h, string path, string cutPath)
		{
			try
			{
				Bitmap b = new Bitmap(path);

				Bitmap cuted = KiCut(b, x, y, w, h);
				cuted.Save(cutPath, System.Drawing.Imaging.ImageFormat.Jpeg);
				cuted.Dispose();
				b.Dispose();
			}
			catch { }
		}

		///<summary>
		/// 剪裁 -- 用GDI+
		///</summary>
		///<param name="b">原始Bitmap</param>
		///<param name="StartX">开始坐标X</param>
		///<param name="StartY">开始坐标Y</param>
		///<param name="iWidth">宽度</param>
		///<param name="iHeight">高度</param>
		///<returns>剪裁后的Bitmap</returns>
		public Bitmap KiCut(Bitmap b, int X, int Y, int Width, int Height)
		{
			if (b == null)
			{
				return null;
			}

			int w = b.Width;
			int h = b.Height;

			if (X >= w || Y >= h)
			{
				return null;
			}

			if (X + Width > w)
			{
				Width = w - X;
			}

			if (Y + Height > h)
			{
				Height = h - Y;
			}

			try
			{
				Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

				Graphics g = Graphics.FromImage(bmpOut);
				g.DrawImage(b, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);

				g.Dispose();
				Bitmap bmpOut2 = new Bitmap(w, h, PixelFormat.Format24bppRgb);
				Graphics c = Graphics.FromImage(bmpOut2);

				c.DrawImage(bmpOut, new System.Drawing.Rectangle(0, 0, w, h), new System.Drawing.Rectangle(0, 0, Width, Height), GraphicsUnit.Pixel);


				c.Dispose();
				b.Dispose();
				return bmpOut2;
			}
			catch
			{
				return null;
			}
		}

		/// <summary>
		/// 成功
		/// </summary>
		public const int Success = 1;
		/// <summary>
		/// 失败
		/// </summary>
		public const int Error = 0;


		/// <summary>
		/// 成功
		/// </summary>
		public const int Success_V1 = 1;
		/// <summary>
		/// 失败
		/// </summary>
		public const int Error_V1 = 0;


		/// <summary>
		/// 你无此操作权限
		/// </summary>
		public const string authdataerror = "你无此操作权限！";
		/// <summary>
		/// 上传文件不允许为空
		/// </summary>
		public const string Scenic_Uploaderror0 = "上传文件不允许为空！";
		/// <summary>
		/// 上传图片格式不对
		/// </summary>
		public const string Scenic_UploaderrorFormat = "上传图片格式不对！";
		/// <summary>
		/// 上传附件格式不对
		/// </summary>
		public const string Att_UploaderrorFormat = "上传附件格式不对！";
		/// <summary>
		/// 上次合同失败
		/// </summary>
		public const string Contract_UpdateError = "上传合同失败";
	}
}
